﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Reset password</title>
    <script src="md5.js" type="text/javascript"></script>
</head>
<body>
    <h3 class="fg-color-dark">
        Please enter your new password:
    </h3>
    <h3 class="icon-locked fg-color-white"> Password</h3>
    <div class="input-control text ">
        <input id="idPasswordUp" type="password" class="fg-color-black" placeholder='Enter your password' style="color:black;" />
    </div>

    <h3 class="icon-locked fg-color-white"> Password Again</h3>
    <div class="input-control text ">
        <input id="PasswordAgain" type="password" class="fg-color-black" placeholder='Enter your password again' style="color:black;" />
    </div>
    <h3 class="fg-color-red icon-eye" style=" border:1px dotted red; display:none;" id='idResetError'></h3>
    <button id="idSubmit" class=" bg-color-darken  fg-color-white icon-unlocked"> Submit </button>
    <img id="idSubmitting" src="../MetroUI/images/preloader-w8-cycle-black.gif" width="18px" height="18px" style="display:none; alt="waiting""/> 
</body>
<script type="text/javascript">
    $(document).ready(function ()
    {
        $('#idSubmit').click(function ()
        {
            if (CheckLength($('#idPasswordUp')) && CheckLength($('#PasswordAgain')))
            {
                if (!CheckTheSamePassword($('#idPasswordUp'), $('#PasswordAgain')))
                {
                    $('#idResetError').hide();
                    $('#idResetError').html("");

                    //save the new password back database
                    SaveAndLogin(GetEmail(), $('#idPasswordUp').val());
                }
            }
        });
    });

    function GetEmail()
    {
        var url = window.location;

        if (url.toString().indexOf("?") != -1)
        {
            var param = url.toString().split('?')[1].split('&');
            var code = param[1].split("=")[1];
            var v = decodeURI(code).split('|');
            var email = v[0];
            return email;
        }
        return "";
    }

    function SaveAndLogin(email, p)
    {
        $('#idSubmitting').show();
        $('#idSubmit').addClass("disabled");

        $.ajax({
            type: "POST",
            url: "Login.ashx",
            data: "mode=ResetPassword&id=" + email + "&p=" +hex_md5( p),
            async: true,
            success: function (data)
            {
                if ('True' == data)
                {
                    //welcome screen, then help screen, then go to main page
                    relocate(email);
                }
                else
                {
                    $('#idResetError').show();
                    $('#idResetError').html("Server is busy, please try it later!");
                }
                $('#idSubmitting').hide();
                $('#idSubmit').removeClass("disabled");
            }
        });
    }

    function CheckLength(obj)
    {
        if (obj.val().length < 4 || obj.val().length > 32)
        {
            $('#idResetError').show();
            $('#idResetError').html("Length of password must be between 4 and 32!");
            return false;
        }

        return true;
    }

    function CheckTheSamePassword(obj, objRepeat)
    {
        var IsError = !(obj.val() == objRepeat.val());
        if (IsError == true)
        {
            $('#idResetError').show();
            $('#idResetError').html("The passwords you typed do not match. Type the same password in both text boxes.");
        }
        return IsError;
    }
</script>
</html>
